#!/bin/sh

ACTION="$1"
CALLER="${2:-$0}"

ansi_red="$(printf "\033[1;31m")"
ansi_white="$(printf "\033[1;37m")"
ansi_green="$(printf "\033[1;32m")"
ansi_yellow="$(printf "\033[1;33m")"
ansi_blue="$(printf "\033[1;34m")"
ansi_bell="$(printf "\007")"
ansi_blink="$(printf "\033[5m")"
ansi_std="$(printf "\033[m")"
ansi_rev="$(printf "\033[7m")"
ansi_ul="$(printf "\033[4m")"

start() {
	{ [ "${CRITICAL}" != "yes" ] && [ "${CALLER}" = "cron" ]; } && return 7
	[ "${ENABLED}" != "yes" ] && return 8
	printf "%s" "${ansi_white} Starting ${DESC}... ${ansi_std}"
	if [ -n "$(pidof "${PROC}")" ]; then
		printf "%s\n" "            ${ansi_yellow} already running. ${ansi_std}"
		return 0
	fi
	if [ "${PROC}" != "AdGuardHome" ] || ! grep -q '^port=553' /etc/dnsmasq.conf; then { ${PRECMD} >/dev/null 2>&1; }; fi
	{ ${PREARGS} ${PROC} ${ARGS} >/dev/null 2>&1 </dev/null & }
	#echo ${PREARGS} ${PROC} ${ARGS}
	local COUNTER="0" LIMIT="10"
	while { [ -z "$(pidof "${PROC}")" ] && [ "${COUNTER}" -le "${LIMIT}" ]; }; do
		sleep 1s
		COUNTER="$((COUNTER + 1))"
	done
	if [ "${PROC}" != "AdGuardHome" ] || ! grep -q '^port=553' /etc/dnsmasq.conf; then { ${POSTCMD} >/dev/null 2>&1; }; fi
	if [ -z "$(pidof "${PROC}")" ]; then
		printf "%s\n" "            ${ansi_red} failed. ${ansi_std}"
		logger -st "${CALLER}" "Failed to start ${DESC} from ${CALLER}."
		return 255
	else
		{ printf "%s\n" "            ${ansi_green} done. ${ansi_std}"; }
		logger -st "${CALLER}" "Started ${DESC} from ${CALLER}."
		return 0
	fi
}

stop() {
	case "${ACTION}" in
	"stop" | "restart")
		local COUNTER="0" LIMIT="10"
		while { [ -n "$(pidof "${PROC}")" ] && [ "${COUNTER}" -le "${LIMIT}" ]; }; do
			[ "${COUNTER}" = "0" ] && printf "%s" "${ansi_white} Shutting down ${PROC}... ${ansi_std}"
			[ -z "$(pidof "${PROC}")" ] && break || { kill -s 2 "$(pidof "${PROC}")" 2>/dev/null || killall -q -2 "${PROC}" 2>/dev/null; }
			sleep "${LIMIT}"s
			COUNTER="$((COUNTER + 1))"
		done
		;;
	"kill")
		printf "%s" "${ansi_white} Killing ${PROC}... ${ansi_std}"
		{ kill -s 2 "$(pidof "${PROC}")" 2>/dev/null || killall -q -2 "${PROC}" 2>/dev/null; }
		;;
	esac
	if [ -n "$(pidof "${PROC}")" ]; then
		printf "%s\n" "            ${ansi_red} failed. ${ansi_std}"
		return 255
	else
		printf "%s\n" "            ${ansi_green} done. ${ansi_std}"
		return 0
	fi
}

check() {
	printf "%s" "${ansi_white} Checking ${DESC}... "
	if [ -n "$(pidof "${PROC}")" ]; then
		printf "%s\n" "            ${ansi_green} alive. ${ansi_std}"
		return 0
	else
		printf "%s\n" "            ${ansi_red} dead. ${ansi_std}"
		return 1
	fi
}

reload() {
	SIGNAL="HUP"
	printf "%s\n" "${ansi_white} Sending ${SIGNAL} to ${PROC}... ${ansi_std}"
	{ kill -s "${SIGNAL}" "$(pidof "${PROC}")" 2>/dev/null || killall -"${SIGNAL}" "${PROC}" 2>/dev/null; }
}

for PROC in ${PROCS}; do
	DESC="${DESC:-$PROC}"
	case "${ACTION}" in
	"start")
		start
		;;
	"stop" | "kill")
		check && stop
		;;
	"restart")
		{ check >/dev/null; } && stop
		start
		;;
	"check")
		check
		;;
	"reload")
		{ check >/dev/null; } && reload
		;;
	*)
		printf "%s\n" "${ansi_white} Usage: $0 (start|stop|restart|check|kill|reload)${ansi_std}"
		exit 1
		;;
	esac
done

#logger "Leaving ${0##*/}."
